﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GDCal.vo;
using GDCal.dao;
using System.Collections;
using ESRI.ArcGIS.Geometry;

namespace GDCal
{
    public partial class AreaTypeDetailForm : Form
    {
        private AreaTypeVO areaTypeVO;

        public AreaTypeDetailForm(AreaTypeVO areaTypeVO)
        {
            Int32 currentId = 0;
            InitializeComponent();
            if (areaTypeVO != null)
            {
                this.areaTypeVO = areaTypeVO;
                this.areaTypeValue.Text = this.areaTypeVO.getType();
                this.areaTypeDescValue.Text = this.areaTypeVO.getDescription();
                currentId = this.areaTypeVO.getId();
                this.isDefault.Checked = this.areaTypeVO.getIsDefault() == 1;
                this.areaGroupVal.Value = this.areaTypeVO.getAreaGroup();

                Int32 foundId = AreaTypeDao.getAreaIdOfAreaGroupHasDefault(Convert.ToInt32(this.areaGroupVal.Value));
                if (foundId > 0)
                {
                    if (this.areaTypeVO.getId() <= 0)
                        this.isDefault.Checked = false;
                    if (this.areaTypeVO.getId() != foundId)
                        this.isDefault.Enabled = false;
                }
                else
                {
                    this.isDefault.Enabled = true;
                }
            }
        }

        public AreaTypeVO getAreaTypeVO()
        {
            return this.areaTypeVO;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.areaTypeValue.Text))
            {
                AreaTypeVO temp = AreaTypeDao.getAreaTypeByType(this.areaTypeValue.Text);
                if (temp != null && temp.getId() > 0 && temp.getId() != this.areaTypeVO.getId())
                {
                    MessageBox.Show(GDCalMessages.Notify_AreaTypeDuplicated, GDCalMessages.Notify_Caption);
                }
                else
                {
                    this.areaTypeVO.setType(this.areaTypeValue.Text);
                    this.areaTypeVO.setDescription(this.areaTypeDescValue.Text);
                    this.areaTypeVO.setAreaGroup(Convert.ToInt32(this.areaGroupVal.Value));

                    if (this.isDefault.Checked)
                        this.areaTypeVO.setIsDefault(1);
                    else
                        this.areaTypeVO.setIsDefault(0);

                    if (this.areaTypeVO.getId() > 0)
                    {
                        AreaTypeDao.updateAreaType(areaTypeVO);
                    }
                    else
                    {
                        AreaTypeDao.insertAreaType(this.areaTypeVO);
                        this.areaTypeVO = AreaTypeDao.getAreaTypeByType(this.areaTypeVO.getType());
                    }
                    this.Close();
                }
            }
            else
            { 
                MessageBox.Show(GDCalMessages.Notify_AreaTypeEmpty, GDCalMessages.Notify_Caption);
            }
        }

        private void areaGroupVal_ValueChanged(object sender, EventArgs e)
        {
            Int32 currentId = AreaTypeDao.getAreaIdOfAreaGroupHasDefault(Convert.ToInt32(this.areaGroupVal.Value));
            if (currentId > 0)
            {
                if (this.areaTypeVO.getId() <= 0)
                    this.isDefault.Checked = false;
                if (this.areaTypeVO.getId() != currentId)
                    this.isDefault.Enabled = false;
            } else 
            {
                this.isDefault.Enabled = true;
            }
        }
    }
}
